package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.bo.AddressDO;
import com.partners.dao.dto.AddressInputDTO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.service.AddressService;
import com.partners.utils.ApiResultUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址管理
 * @author liangyongle
 * @date 2019-01-12
 */
@RestController
@RequestMapping("address")
public class AddressCotroller {


    @Resource
    private AddressService addressService;


    /**
     * 根据uid获取地址列表
     * @param uid Integer
     * @return AddressDO
    * */
    @PostMapping("get_address")
    public ApiResult<List<AddressDO>> getAddress(@SimpleParam("uid") Integer uid){
      if(uid == null){
          throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
      }
      List<AddressDO> addressDO = addressService.getAddressListByUid(uid);
      return ApiResultUtil.ok(addressDO);
    }

    /**
     * 编辑地址
     * @param addressInputDTO AddressInputDTO
     * @return boolean
     * */
    @PostMapping("edit_address")
    public ApiResult eidtAddress(@BeanValid @RequestBody AddressInputDTO addressInputDTO){
       addressService.editAddress(addressInputDTO);
       return ApiResultUtil.ok(SuccessMessageEnum.EDIT_SUCCESS);
    }


    /**
     * 获取地址详情
     * @param id Integer
     * @return boolean
     * */
    @PostMapping("address_detail")
    public ApiResult<AddressDO> addressDetail(@SimpleParam("id") Integer id){
        if(id == null){
            throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);
        }
        AddressDO address = addressService.getAddressInfo(id);
        return ApiResultUtil.ok(address);
    }

}
